using System;
using ASTClass;

namespace Taste 
{
    class Taste 
    {
	    public static void Main (string[] arg) 
        {
            Scanner scanner;
            string file;
            Console.Write("Input filename> ");
            file = Console.ReadLine();
            while (file != "")
            {
                try
                {
                    CodeFileGen cfg;
                    SymbolTable symTab = new SymbolTable();
                    try
                    {
                        scanner = new Scanner(file);
                        cfg = new CodeFileGen(file + ".asm", file);
                    }
                    catch
                    {
                        Console.WriteLine("File: {0} doesn't exist.", file);
                        return;
                    }

                    Parser parser = new Parser(scanner);

                    parser.Parse();
                    if (parser.errors.count == 0)
                    {
                        Console.WriteLine("Input file is accepted");

                        // Make graphviz dot file of the AST
                        //parser.root.MakeDotFile("AST.dot", "Taste Prog AST");
                        parser.root.Simplify();
                        parser.root.MakeDotFile("AST.dot", "Taste Prog Simplified AST");

                        // Run the graph viz batch file to generate JPG file from AST.dot
                        System.Diagnostics.Process proc;
                        proc = System.Diagnostics.Process.Start("chartJPG.bat");

                        // Use the default JPG viewer (e.g. Windows Photo Viewer)
                        System.Diagnostics.Process.Start("AST.jpg");
                        parser.root.CodeGen(symTab, cfg);
                        cfg.ToFile();
                    }
                    else
                        Console.WriteLine("Input file is not accepted");
                }
                catch (Exception)
                {
                    Console.WriteLine("Error Generating Code.");
                }

                Console.Write("\n\nInput filename> ");
                file = Console.ReadLine();
            }
	    }	
    } // end class
} // end namespace
